package com.wyj.chat.websocket_chat.controller;

import com.wyj.chat.websocket_chat.pojo.Information;
import com.wyj.chat.websocket_chat.pojo.User;
import com.wyj.chat.websocket_chat.service.InformationService;
import com.wyj.chat.websocket_chat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class InformationController {

    @Autowired
    private InformationService informationService;
    
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/information/getInformation", method = RequestMethod.POST)
    @ResponseBody
    public Information getInformation(String id) {
        Information information = informationService.getById(id);
        return information;
    }

    @RequestMapping(value = "/information/saveInformation", method = RequestMethod.POST)
    @ResponseBody
    public boolean saveInformation(Information information) {
        if (StringUtils.isEmpty(information.getId())) {
            return false;
        }
        Information information1 = informationService.getById(information.getId());
        int i ;
        if (information1 == null) {
            i = informationService.insertInformation(information);
        } else {
            i = informationService.updateInformation(information);
        }
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }

    @RequestMapping(value = "/information/updateInformation", method = RequestMethod.POST)
    @ResponseBody
    public boolean updateInformation(Information information,String name) {
        System.out.println(information);
        if (name.isEmpty()) {
            return false;
        }
        User user = userService.getByName(name);
        information.setId(user.getId());
        int flag = informationService.updateInformation(information);
        if (flag == 0) {
            // 修改失败
            return false;
        }
        return true;
    }

}
